package com.art.sunflower.service.impl;

import com.art.sunflower.exception.*;
import com.art.sunflower.mapper.ClassesMapper;
import com.art.sunflower.pojo.Classes;
import com.art.sunflower.pojo.Enroll;
import com.art.sunflower.pojo.Lay;
import com.art.sunflower.service.ClassesService;
import com.art.sunflower.util.MyPageInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ClassesServiceImpl implements ClassesService {
    @Autowired
    @Resource
    private ClassesMapper classesMapper;

    public int insertClasses(String classesName, int courseId, int teacherId, String startweek, String endweek, int truefee, int maxNum) {
        return classesMapper.insertClasses(classesName, courseId, teacherId, startweek, endweek, truefee, maxNum);
    }

    public int updateClasses(int id, String classesName, int courseId, int teacherId, String startweek, String endweek, int truefee, int maxNum) {
        return classesMapper.updateClasses(id, classesName, courseId, teacherId, startweek, endweek, truefee, maxNum);
    }

    public int deleteClasses(int id) {
        return classesMapper.deleteClasses(id);
    }

    public Map<String, Object> selectOneClasses(int id) {
        return classesMapper.selectOneClasses(id);
    }

    public Map<String, Object> selectOneClasses2(int id) {
        return classesMapper.selectOneClasses2(id);
    }

    public MyPageInfo listAllClasses(Integer pagenum, Integer pagesize) throws BusyException {
        //需要进行分页
        PageHelper.startPage(pagenum, pagesize);
        List<Map<String, Object>> classes = classesMapper.listAllClasses();
        List<Map<String, Object>> classes2 = classesMapper.listAllClasses2();
        if (classes.size() == classes2.size()) {
            for (int i = 0; i < classes.size(); i++) {
                classes.get(i).put("courseName", classes2.get(i).get("courseName"));
            }
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(classes);
            return new MyPageInfo(pageInfo);
        } else {
            throw new BusyException();
        }
    }

    public MyPageInfo listAllClasses2(Integer pagenum, Integer pagesize) {
        /*List<Map<String, Object>> classes = classesMapper.listAllClasses2(pagenum,pagesize);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(classes);
        return new MyPageInfo(pageInfo);*/
        return null;
    }

    public int updateOpenClasses(int id, String classesName, int courseId, int teacherId, String startweek, String endweek, int truefee, int maxNum) {
        return classesMapper.updateOpenClasses(id, classesName, courseId, teacherId, startweek, endweek, truefee, maxNum);
    }

    public int addOpenClasses(String classesName, int courseId, int teacherId, String startweek, String endweek, int truefee, int maxNum) {
        return classesMapper.addOpenClasses(classesName, courseId, teacherId, startweek, endweek, truefee, maxNum);
    }

    public int deleteOpenClasses(int id) {
        return classesMapper.deleteOpenClasses(id);
    }

    public Map<String, Object> selectOneOpenClasses(int id) {
        return classesMapper.selectOneClasses(id);
    }

    public Map<String, Object> selectOneOpenClasses2(int id) {
        return classesMapper.selectOneClasses2(id);
    }

    public MyPageInfo listAllOpenClasses(Integer pagenum, Integer pagesize) throws BusyException {
        //需要进行分页
        PageHelper.startPage(pagenum, pagesize);
        List<Map<String, Object>> classes3 = classesMapper.listAllOpenClasses();
        List<Map<String, Object>> classes4 = classesMapper.listAllOpenClasses2();
        if (classes3.size() == classes4.size()) {
            for (int i = 0; i < classes3.size(); i++) {
                classes3.get(i).put("course_name", classes4.get(i).get("course_name"));
            }
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(classes3);
            return new MyPageInfo(pageInfo);
        } else {
            throw new BusyException();
        }
    }

    public MyPageInfo listAllOpenClasses2(Integer pagenum, Integer pagesize) {
        return null;
    }

    public Lay findClassesByTeacher(int teacherId, Integer pagenum, Integer pagesize) {
        //需要进行分页
        PageHelper.startPage(pagenum, pagesize);
        List<Map<String, Object>> classesList3 = (List<Map<String, Object>>) classesMapper.findClassesByTeacher(teacherId, pagenum, pagesize);
        PageInfo info = new PageInfo(classesList3);//创建pageinfo，包含分页的信息
        Lay lay2 = new Lay();
        lay2.setPagenum(pagenum);
        lay2.setCount(info.getTotal());//总条数
        lay2.setData(info.getList());//显示的数据
        return lay2;
    }

    public int openClasses(int id, int trueNum, int maxNum, boolean status) {
        return classesMapper.openClasses(id, trueNum, maxNum, status);
    }

    @Override
    public void enroll(Enroll enroll) throws EnableClassesException, EnrollMaxException, NullClassesException, InconformityEnrollException {
        Classes classes = classesMapper.selectClasses(enroll.getClassesId());
        if (classes == null) {
            throw new NullClassesException();
        } else if (!classes.isStatus()) {
            throw new EnableClassesException();
        } else if (classes.getTrueNum().equals(classes.getMaxNum())) {
            throw new EnrollMaxException();
        } else {
            Map<String, Object> map = classesMapper.selectEnroll(classes.getId());
            if (map.get("grade") != map.get("requirement")) {
                throw new InconformityEnrollException();
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String date = df.format(new Date());// new Date()为获取当前系统时间
            classesMapper.insertClassesOrder(enroll.getStudentId(), enroll.getClassesId(), enroll.getFee(), date);
            classes.setTrueNum(classes.getTrueNum() + 1);
            classesMapper.updateTrueNum(enroll.getClassesId(), classes.getTrueNum());

        }
    }

    @Override
    public Map<String, Object> getClasses(Integer id) throws NullClassesException {
        Classes classes = classesMapper.selectClasses(id);
        if (classes == null) {
            throw new NullClassesException();
        } else {
            return classesMapper.selectClassesInfor(id);
        }
    }

}
